<template>
  <div>
    <div class="pc-content">
      <div class="main-txt" v-show="routerList.length != 0">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item
            v-for="breadItem in breadcrumb"
            :key="breadItem.name"
            >{{ breadItem.name }}</el-breadcrumb-item
          >
        </el-breadcrumb>
        <!-- <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item
            v-for="breadItem in breadLine"
            :key="breadItem.id"
            :to="{ name: breadItem.name }"
            @click.native="changeBread(breadItem)"
            >{{ breadItem.val }}</el-breadcrumb-item
          >
        </el-breadcrumb> -->
      </div>
      <!-- <div class="middle-rigth" v-show="routerList.length != 0">
        <slipeNav
          :navList="routerList"
          @changehandleNav="changehandleNav"
        ></slipeNav>
      </div> -->
      <div class="main-top" v-show="routerList.length != 0">
        <div
          v-show="scrollIdx > 0 && routerList.length > 6"
          class="prev"
          @click="prevBtn"
        >
          <i class="el-icon-arrow-left"></i>
        </div>
        <div class="main-all" ref="mainAll">
          <button
            class="main-item"
            v-for="(routerItem,index) in routerList"
            :key="routerItem.busTypeId"
            :class="{ mainActive: curentId == routerItem.busTypeId || index===0&&curentId===1}"
            @click="changeBtns(routerItem)"
          >
            <router-link
              :to="{
                name: 'allFeatures',
                query: {
                  titleId: $route.query.titleId,
                  busTypeId: routerItem.busTypeId,
                  osVersionId: $route.query.osVersionId,
                  osTypeId: $route.query.osTypeId,
                },
              }"
              >{{ routerItem.name }}</router-link
            >
          </button>
        </div>
        <div
          v-show="scrollIdx < routerList.length - 6 && routerList.length > 6"
          class="next"
          @click="nextBtn"
        >
          <i class="el-icon-arrow-right"></i>
        </div>
      </div>
      <router-view></router-view>
    </div>
    <!-- <div class="mobile-content"></div> -->
  </div>
</template>

<script>
import { mapActions, mapState } from "vuex";
import slipeNav from "./commonComponent/slipeNav.vue";
export default {
  components: { slipeNav },
  data() {
    return {
      activeIndex: "1",
      activeIndex2: "1",
      list: [{}, {}, {}, {}, {}, {}, {}, {}, {}],
      // routerList: [
      //   { busTypeId: 1, name: "UI1" },
      //   { busTypeId: 2, name: "媒体2" },
      //   { busTypeId: 3, name: "网络与链接3" },
      //   { busTypeId: 4, name: "UI4" },
      //   { busTypeId: 5, name: "媒体5" },
      //   { busTypeId: 6, name: "网络与链接6" },
      //   { busTypeId: 7, name: "网络与链接7" },
      //   { busTypeId: 8, name: "UI8" },
      //   { busTypeId: 9, name: "媒体9" },
      // ],
      scrollIdx: 0, // 滚动的次数
      // breadLine: [
      //   { id: 0, val: "应用开发", name: "content" },
      //   { id: 1, val: "入门级", name: "mainText" },
      //   { id: 2, val: "UI", name: "filterUi" },
      // ],
      filterId: 1,
      firstLevel: "",
      curentId: 0,
      titleId: 1,
      busTypeId: null,
      osTypeId: null,
      osVersionId: null,
      pageNo: 1,
      pageSize: 9,
      number: 1,
      val: 0,
      tableView: [],
      elMainAll: "",
    };
  },
  created() {},
  mounted() {
    // this.searchData(1);
    this.elMainAll = this.$refs["mainAll"];
    window.addEventListener("resize", this.scrollFn);
  },
  computed: {
    ...mapState({
      breadcrumb: (state) => state.breadcrumb.breadData,
      routerList: (state) => state.initData.busTypeList,
    }),
  },
  methods: {
    ...mapActions(["setBreadcrumb", "getInitData"]),
    async searchAll(busTypeId) {
      if (this.$route.query.titleId) {
        this.titleId = this.$route.query.titleId;
      }
       if (this.$route.query.osTypeId) {
        this.osTypeId = this.$route.query.osTypeId;
      }
       if (this.$route.query.osVersionId) {
        this.osVersionId = this.$route.query.osVersionId;
      }
      let res = await this.$axios.get(
        "/knowledge/frontPage/queryInitialization",
        {
          params: {
            titleId: this.titleId,
            busTypeId: busTypeId,
            osTypeId: this.osTypeId,
            osVersionId: this.osVersionId,
            pageNum: this.pageNo,
            pageSize: this.pageSize,
          },
        }
      );
      if (res.data.code == 0) {
        let busTypeList = res.data.data.busTypeList;
        let featuresList = res.data.data.featuresList;
        let sampleList = res.data.data.sampleList;
        let titleList = res.data.data.titleList;
        let osTypeList = res.data.data.osTypeList;
        let osVersionList = res.data.data.osVersionList;
        let totalNum = res.data.totalNum;
        osTypeList.unshift({
          osTypeId: "0",
          name: "全部",
          sort: "1",
        });
        osVersionList.unshift({
          osVersionId: "0",
          name: "全部",
          sort: "1",
        });
        this.getInitData({
          busTypeList,
          featuresList,
          sampleList,
          titleList,
          osTypeList,
          osVersionList,
          totalNum,
        });
      }
    },

    changeBtns(routerItem) {
      this.curentId = routerItem.busTypeId;
      this.busTypeId = routerItem.busTypeId;
      this.searchAll(this.busTypeId);
      this.setBreadcrumb({type: 'bustype', ...routerItem})
    },
    prevBtn() {
      let width = this.elMainAll.clientWidth;
      if (this.scrollIdx > 0) {
        this.scrollIdx--;
        console.log("scrollIdx", this.scrollIdx);
        this.elMainAll.scrollLeft = this.scrollIdx * Math.round(width / 6);
      } else {
        console.warn("已没有更多了");
      }
    },
    nextBtn() {
      let width = this.elMainAll.clientWidth;
      if (this.scrollIdx < this.routerList.length - 6) {
        this.scrollIdx++;
        console.log("scrollIdx", this.scrollIdx);
        this.elMainAll.scrollLeft = this.scrollIdx * Math.round(width / 6);
      } else {
        console.warn("已没有更多了");
      }
    },
    scrollFn() {
      let width = this.elMainAll.clientWidth;
      this.elMainAll.scrollLeft = this.scrollIdx * Math.round(width / 6);
    },
  },
  beforeDestroy() {
    window.removeEventListener("resize", this.scrollFn);
  },
};
</script>
<style lang="less" scoped>
.mobile-content {
  display: none !important;
}
.pc-content {
  display: block !important;
  width: 100%;
  height: 100%;
  // display: flex;
  // flex-direction: column;
  .main-txt {
    min-height: 22px;
    margin-top: 40px;
    ::v-deep .el-breadcrumb__item {
      font-size: 14px;
      font-family: HarmonyOS_Sans_SC;
      color: rgba(0, 0, 0, 1) !important;
      line-height: 22px;
      opacity: 0.5;
    }
  }
  .main-top {
    width: 1180px;
    // width: 74.5%;
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    flex-wrap: wrap;
    border-bottom: 1px solid rgba(0, 0, 0, 0.2);
    position: relative;
    .prev {
      width: 50px;
      height: 70px;
      line-height: 70px;
      text-align: center;
      color: #000000;
    }

    .main-all {
      // flex: 1;
      // position: absolute;
      // overflow:hidden;
      min-height: 70px;
      width: 100%;
      white-space: nowrap;
      overflow: hidden;
      .main-item {
        width: calc(100% / 6);
        height: 70px;
        font-size: 20px;
        line-height: 70px;
        border: 0;
        background-color: #ffffff;
        overflow: hidden;
        text-overflow: ellipsis;
        // border-bottom: 1px solid rgba(0, 0, 0, 0.2);
        ::v-deep a {
          color: #000000;
          line-height: 70px;
          text-decoration: none;
          // display: block;
          height: inherit;
          width: inherit;
          opacity: 0.9;
        }
        a {
          line-height: 70px;
          text-decoration: none;
          display: inline-block;
          height: 100%;
          width: 100%;
        }
      }
      .mainActive {
        border-bottom: 2px solid #000000;
      }
    }
    .prev {
      position: absolute;
      left: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 50px;
      height: 70px;
      line-height: 70px;
      text-align: left;
      color: #000000;
      cursor: pointer;
    }
    .next {
      position: absolute;
      right: 0;
      top: 50%;
      transform: translateY(-50%);
      width: 50px;
      height: 70px;
      line-height: 70px;
      text-align: right;
      color: #000000;
      cursor: pointer;
    }
  }
}

@media screen and (max-width: 540px) {
  .pc-content {
    width: 100%;
    height: 100%;
    display: block !important;
    .main-txt {
      display: none;
    }
    .main-top {
      // display: none;
      margin: 0 12px;
      width: auto;
      height: 40px;
      display: flex;
      flex-direction: row;
      justify-content: space-between;
      border-bottom: 1px solid rgba(0,0,0,0.06);
      .prev {
        display: none;
      }
      .next {
        display: none;
      }
      .main-all {
        height: 40px;
        min-height: 40px;
        width: 100%;
        // width: 1180px;
        white-space: nowrap;
        -webkit-overflow-scrolling: touch;
        overflow-x: auto;
        overflow-y: hidden;
        overflow: -moz-scrollbars-none;
        // position: absolute;
        .main-item {
          // width: 100%;
          width: 100px;
          height: 40px;
          font-size: 14px;
          text-align: center;
          line-height: 40px;
          border: 0;
          // border: 1px solid black;
          background-color: #ffffff;
          // border-bottom: 1px solid rgba(0, 0, 0, 0.2);
          ::v-deep a {
            color: #000000;
            opacity: 0.6;
          }
          a {
            line-height: 40px;
            text-decoration: none;
            display: inline-block;
            height: inherit;
            width: inherit;
          }
        }
        .mainActive {
          border-bottom: 2px solid #000000;
          a{
              opacity: 0.9
          }
        }
      }
      .main-all::-webkit-scrollbar {
        display: none;
      }
    }
  }
}
</style>
